package com.itheima.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.Code;
import com.itheima.common.MessageConstant;
import com.itheima.common.PageResult;
import com.itheima.common.Result;

import com.itheima.domain.Setmeal;
import com.itheima.domain.SetmealDTO;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: reggie_take_out
 * @className: SetmealController
 * @description: Reggie后端接口开发
 * @author: psh
 * @create: 2022-06-06 10:09
 **/
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

	@Autowired
	private SetmealService setmealService;
	/**
	 * 功能描述: 套餐列表分页展示
	 * @param page
	 * @param pageSize
	 * @param name
	 * @return : com.itheima.common.PageResult
	*/
	@GetMapping("/page")
	public PageResult page(@RequestParam("page") Integer page,
						   @RequestParam("pageSize") Integer pageSize,
						   @RequestParam(value = "name",required = false) String name){
		//1.调用Service层获取业务处理之后的结果
		Page<Setmeal> pageResult = setmealService.page(page,pageSize,name);
		//2.检查分页查询结果
		if(pageResult == null){
			return new PageResult(Code.ERROR, MessageConstant.SETMEAL_GET_ERROR);
		}
		//3.获取当前页列表数据、总条数
		List<Setmeal> records = pageResult.getRecords();
		long total = pageResult.getTotal();
		//4.封装返回结果
		return new PageResult(Code.OK,MessageConstant.SETMEAL_GET_SUCCESS,records,total);
	}
	/**
	 *@Description: 新增套餐
	 *@Param: SetmealDTO setmealDTO
	 *@return: int
	 *@Author: psh
	 *@Date: 2022/6/6 12:51
	 */
	@PostMapping
	public Result save(@RequestBody SetmealDTO setmealDTO){
		int row = setmealService.save(setmealDTO);
		if (row < 1){
			return new Result(Code.OK,MessageConstant.SETMEAL_SAVE_ERROR);
		}
		return new Result(Code.OK,MessageConstant.SETMEAL_SAVE_SUCCESS);
	}
	/**
	 *@Description: 修改套餐
	 *@Param: SetmealDTO setmealDTO
	 *@return: int
	 *@Author: psh
	 *@Date: 2022/6/6 14:02
	 */
	@PutMapping
	public Result update(@RequestBody SetmealDTO setmealDTO){
		int row = setmealService.update(setmealDTO);
		if (row < 1){
			return new Result(Code.OK,MessageConstant.SETMEAL_UPDATE_ERROR);
		}
		return new Result(Code.OK,MessageConstant.SETMEAL_UPDATE_SUCCESS);
	}
	/**
	 *@Description: 通过id获取SetmealDTO对象
	 *@Param: Long id
	 *@return: SetmealDTO
	 *@Author: psh
	 *@Date: 2022/6/11 0:23
	 */
	@GetMapping("/{id}")
	public Result findById(@PathVariable("id") Long id){
		SetmealDTO result = setmealService.findById(id);
		if(result == null){
			return new Result(Code.ERROR,MessageConstant.SETMEAL_GET_ERROR);
		}
		return new Result(Code.OK,MessageConstant.SETMEAL_GET_SUCCESS,result);
	}
	/**
	*@Description: 批量启停、启售
	*@Param: Integer status  Long[] ids
	*@return: Result
	*@Author: psh
	*@Date: 2022/6/7 15:43
	*/
	@PostMapping("/status/{status}")
	public Result updateStatusByIds(@PathVariable("status") Integer status,
									@RequestParam("ids") Long[] ids){
		setmealService.updateStatusByIds(status,ids);
		return new Result(Code.OK,MessageConstant.SETMEAL_UPDATE_SUCCESS);
	}
	/**
	 *@Description: 批量删除
	 *@Param: Long[] ids
	 *@return: int
	 *@Author: psh
	 *@Date: 2022/6/6 19:05
	 */
	@DeleteMapping
	public Result deleteByIds(@RequestParam("ids") Long[] ids){
		int row = setmealService.deleteByIds(ids);
		if (row < 1){
			return new Result(Code.OK, MessageConstant.SETMEAL_UPDATE_ERROR);
		}
		return new Result(Code.OK, MessageConstant.SETMEAL_UPDATE_SUCCESS);
	}
	/**
	*@Description: 根据分类、状态获取套餐列表
	*@Param: Long categoryId  Integer status
	*@return: Result
	*@Author: psh
	*@Date: 2022/6/7 15:42
	*/
	@GetMapping("/list")
	public Result list(@RequestParam("categoryId") Long categoryId,@RequestParam("status") Integer status){
		//1.调用service,接收service业务处理结果
		List<Setmeal> setmealList =  setmealService.list(categoryId,status);
		//2.封装返回
		if(setmealList == null){
			setmealList = new ArrayList<>();
		}
		return new Result(Code.OK,MessageConstant.SETMEAL_GET_SUCCESS,setmealList);
	}
}
